<jsp:include page="/templates/header.jsp"/>
<%@page import="controller.*"%>
<%@page import="database.Queries"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.sql.Connection" %>
<%@page import="database.Connect" %>
<%@page import="java.sql.SQLException"%>

<%
  Connection con=null;
  try{
	  con=Connect.getConnection();
	  con.setAutoCommit(false);
	  String username = request.getUserPrincipal().getName();
	  Student s = Queries.createStudent(con,username);
	  String name = request.getParameter("name");
	  LinkedList<Course> l = Queries.getCourses(name);
	  Course c=null;
	  if(l.size()!=0)
	  	c=l.get(0);
	  if (c==null){%>	
	    <br/>
		<h1> course with name:<%=name %> doesn't exist in system</h1>
		<br/>
		<a href="courses.jsp">return to courses</a>
	  <%}else {%>
	  	<script type="text/javascript" src="/salonmorandsmigi/js/confirmcourse.js"></script>
	     <%if (c.getCreator().equals(username)){%>
	     	<div align="right">
	  	 		<button id="removecourse" name="<%= name %>">remove course</button>
	  	 	</div>
	  	 <%} %>
	  	 <%if (s.isStudent() && s.canRegisterToCourse(c)){%>
	     	<div align="right">
	  	 		<button id="registertocourse">register to course</button>
	  	 		<div class="username" id="<%=username %>"></div>
	  	 		<div class="coursename" id="<%=name %>"></div>
	  	 	</div>
	  	 <%} %>
	  	<div id="dialog" title="Are you sure?" style="display:none">
			<p>Are you sure you want to delete the course?</p>
		</div>
		<div id="dialog2" title="Are you sure?" style="display:none">
			<p>Are you sure you want to register to the course?</p>
		</div>
	  	 <h1> course Details</h1>
	  	 <div class="details">
		  	 <p><strong>course name:</strong>&nbsp;&nbsp;<%=name%></p>
		  	 <p><strong>description:</strong>&nbsp;&nbsp;<%=c.getDescription()%></p>  
		  	 <p><strong>number of points:</strong>&nbsp;&nbsp;<%=c.getPointNum()%></p> 
		  	 <p><strong>capacity:</strong>&nbsp;&nbsp;<%=c.getCapacity()%></p> 
		  	 <p><strong>group number:</strong>&nbsp;&nbsp;<%=c.getGroupNum()%></p> 
	  	 </div>
	  <%
	  	CourseSession[] sessions = c.getSessions();
	    if (sessions==null){%>
	    	<div class="details">
	    		<h2>The course dont have any sessions yet</h2>
	    	  </div>
	    <%} else {%>
	    	  <div class="details">
	    	  <p><strong>course sessions:</strong>&nbsp;&nbsp;</p>
	    	  	<div class="tablesorter" id="courses" align="center">
				<table border="3" width="30%">
					<tr align="center" id=<%=name%>>
			 			<td> Day </td>
			 			<td> Start hour </td>
			 			<td> End hour </td>
			 		</tr>
		    <%for(int i=0;i<sessions.length;i++){ %>
			 		<tr align="center" id=<%=name%>>
			 			<td> <%=sessions[i].getDay()%> </td>
			 			<td> <%=sessions[i].getStart()%> </td>
			 			<td> <%=sessions[i].getEnd()%> </td>
			 		</tr>
			<% }%>
			 	</table>
			 	</div>
			   </div>
		 <%} %>
		<%}
 }
 catch (SQLException e){
	con.rollback();
	out.write("SERVER ERROR! PLEASE TRY AGAIN");
 }
 finally{
  	if(con!=null) 
  	{
  		con.setAutoCommit(true);
  		 con.close();
  	}
 }%>
		 <br/><br/><br/>
<jsp:include page="/templates/sidebar.jsp"/>
<jsp:include page="/templates/footer.jsp"/>